package spring.cloud.simple.config;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

import spring.cloud.simple.interfaces.UserService;
import spring.cloud.simple.service.UserServiceImpl;

@Configuration
public class ThriftConfig {
    private ExecutorService executor = Executors.newSingleThreadExecutor();

    @Bean
    public TServerTransport tServerTransport() {
        try {
            return new TServerSocket(7911);
        } catch (TTransportException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public TServer tServer() {
        UserService.Processor processor = new UserService.Processor(new UserServiceImpl());
        TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(tServerTransport()).processor(processor));
        return server;
    }

    @PostConstruct
    public void init() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                tServer().serve();
            }
        });
    }
}
